
package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.bo.*;
import com.cskaoyan.bean.entity.Admin;
import com.cskaoyan.bean.entity.User;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CartCheckOutVo;
import com.cskaoyan.bean.vo.CartIndexTransferVo;
import com.cskaoyan.bean.vo.WxCheckoutVO;
import com.cskaoyan.service.UserService;
import com.cskaoyan.service.WxCartService;
import com.cskaoyan.service.WxCartServiceImpl;
import com.cskaoyan.utils.CurrentUser;
import jdk.nashorn.internal.ir.RuntimeNode;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author XuBanga
 * @TODO:
 * @date 2021/10/22$
 * @trainOfthought:
 */
@RestController
@RequestMapping("wx/cart")
public class WxCartController {
    @Autowired
    WxCartService wxCartService;

    @Autowired
    UserService userService;
    /**
     * 浏览商品，初次加入购物车-add
     * Request URL: http://182.92.235.201:8083/wx/cart/add
     *
     * Request Method: POST
     *
     * 请求参数：
     * goodsId: 1181016-----》market_goods_product
     * number: 2
     * productId: 260-----》market_goods_product的id
     *
     * 响应结果：
     * data: 2
     * errmsg: "成功"
     * errno: 0
     *
     * 分析：根据
     */
    @PostMapping("add")
    public BaseRespVo addCart(@RequestBody CartAddJsonBo cartAddJsonBo){
        /**
         * 根据goodsId,在market_goods表中查询，黑圈信息；
         * 根据produce_id，在market_goods_product表中，查询：红圈信息；
         * 将信息存储market_cart表格中
         */
        /**
         * 最终是这样的写法；但还需要深入理解  或者  直接测试，看看 可以不；   暂时假定一个id
         */
        Subject subject = SecurityUtils.getSubject();
        User primaryPrincipal = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = primaryPrincipal.getId();

//        Integer id = Integer.parseInt(request.getSession().getId());   //先假设一个id；
        wxCartService.addCart(cartAddJsonBo,id);

        return BaseRespVo.ok(cartAddJsonBo.getNumber());
    }

    /**
     *购物车内，添加/减少  同一个商品数量-update
     *
     * Request URL: http://182.92.235.201:8083/wx/cart/update
     *
     * Request Method: POST
     *
     * 请求参数：
     * goodsId: 1152009
     * id: 141
     * number: 2
     * productId: 233
     *
     * 响应结果：
     * errmsg: "成功"
     * errno: 0
     */
    @PostMapping("update")
    public BaseRespVo updateCart(@RequestBody CartUpdateJsonBo cartUpdateJsonBo){
        /**
         *根据goodsId和productId,确定购物车market_cart中对应的数据，使用number进行更改
         */

        wxCartService.updateCart(cartUpdateJsonBo);

        return BaseRespVo.ok();
    }


    /**
     *购物车内，直接付款-fastadd
     *
     * Request URL: http://182.92.235.201:8083/wx/cart/fastadd
     *
     * Request Method: POST
     *
     * 请求参数：
     * goodsId: 1181016
     * number: 1
     * productId: 260
     *
     * 响应结果：
     * data: 150
     * errmsg: "成功"
     * errno: 0
     * 注：
     * 伴随着：add、checkout
     *
     * 1.获取goodsId和productId，查询数据，将对应数据插入到购物车中；
     * 2.查询market_cart，返回对应的id;用于响应
     */
    @PostMapping("fastadd")
    public BaseRespVo fastaddCart(@RequestBody CartFastAddJsonBo cartFastAddJsonBo){

        Subject subject = SecurityUtils.getSubject();
        User users = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = users.getId();

        Integer id1 = wxCartService.fastaddCart(cartFastAddJsonBo,id);

        return BaseRespVo.ok(id1);
    }


    /**
     * 点击商品，进入购物车页面-goodscount
     * Request URL: http://182.92.235.201:8083/wx/cart/goodscount
     *
     * Request Method: GET
     *
     * 请求参数：没有
     * 注：
     * 但存在token
     * B0b2tlbiIsImF1ZCI6Ik1JTklBUFAiLCJpc3MiOiJNQVJLRVQiLCJleHAiOjE2MzQ4OTAyOTEsInVzZXJJZCI6MSwiaWF0IjoxNjM0ODgzMDkxfQ.x1OKFPKRvfD_Vgs-s8To7SPwOcWSVPHncDdLOJ2MFNs
     *
     *
     * 响应结果：购物车中商品的总个数  -->  查询cart表，总计商品的个数
     * data: 31
     * errmsg: "成功"
     * errno: 0
     */
    @RequestMapping("goodscount")
    public BaseRespVo goodscountCart(){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getSession().getAttribute("username");
        int userId = userService.getUserId(username);
        int cartGoodsNum = wxCartService.selectGoodsNum(userId);
        return BaseRespVo.ok(cartGoodsNum);
    }

    /**
     *Request URL: http://localhost:8083/wx/cart/index
     *
     * Request Method: GET
     *
     * 请求参数：没有请求参数
     * 注：但存在token
     *
     * 1.根据token，Subject subject = SecurityUtils.getSubject();获取用户id;
     * 2.根据用户id,market_cart查询对应的商品总数和总价格（计算）；--->封装到cartTotal对象中
     * 3.根据用户id，market_cart查询所有对应信息，封装到cartList list中；
     */
    @RequestMapping("index")
    public BaseRespVo indexCart(){
        //获取商品id
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = principal.getId();

        CartIndexTransferVo cartIndexTransferVo = wxCartService.indexCart(id);
        return BaseRespVo.ok(cartIndexTransferVo);
    }

    /**
     * Request URL: http://182.92.235.201:8083/wx/cart/delete
     *
     * Request Method: POST
     *
     * 请求参数：
     * {"productIds":[139,265]}
     * productIds: [139, 265]-------------------当下删除商品的productId
     */
    @PostMapping("delete")
    public BaseRespVo deleteCart(@RequestBody ProductIds productIds){
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = principal.getId();

        CartIndexTransferVo cartIndexTransferVo = wxCartService.deleteCart(id,productIds);
        return BaseRespVo.ok(cartIndexTransferVo);
    }

    /**
     * Request URL: http://182.92.235.201:8083/wx/cart/checked
     *
     * Request Method: POST
     *
     * 请求参数：
     * isChecked: 1--------------------------0：取消  1：选中
     * productIds: [76]---------商品id
     * 0: 76
     * {"productIds":[76],"isChecked":1}
     *
     * 分析：
     * 1.根据用户id,在market_cart中，进而根据productId,修改传入的checked值；
     * 2.根据用户id,productId,checked值，确定如下参数值：
     *  	"goodsCount": 5,-----------------------购物车商品总数
     *  	"checkedGoodsCount": 4,-----------------购物车选中商品总数
     *  	"goodsAmount": 365,------------购物车商品总价
     *  	"checkedGoodsAmount": 236-------------购物车选中商品总价
     * 3.根据用户id,查询market_user中，所有的商品数（可以复用index中的代码，记得已经封装成可以直接使用的函数）
     */
    @PostMapping("checked")
    public BaseRespVo checkedCart(@RequestBody CartCheckedBo cartCheckedBo){
        Subject subject = SecurityUtils.getSubject();
        User principal = (User) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = principal.getId();

        //用户id,请求参数
        CartIndexTransferVo cartIndexTransferVo = wxCartService.checkedCart(id,cartCheckedBo);
        return BaseRespVo.ok(cartIndexTransferVo);
    }

    /**
     * Request URL: http://182.92.235.201:8083/wx/cart/checkout?
     * cartId=0&addressId=0&couponId=0&userCouponId=8&grouponRulesId=0
     *
     * Request Method: GET
     *
     * 请求参数：（5个id，对应数据库中，5个对应的表格）
     * cartId: 274
     * addressId: 19
     * couponId: 0
     * userCouponId: 92
     * grouponRulesId: 0
     */
//    @RequestMapping("checkout")
//    public BaseRespVo checkoutCart(CartCheckoutBo cartCheckoutBo){
//        Subject subject = SecurityUtils.getSubject();
//        User principal = (User) subject.getPrincipals().getPrimaryPrincipal();
//        Integer id = principal.getId();
//
//        //用户id,请求参数
//        CartCheckOutVo cartIndexTransferVo = wxCartService.checkoutCart(id,cartCheckoutBo);
//        return BaseRespVo.ok(cartIndexTransferVo);
//    }
    @RequestMapping("checkout")
    public BaseRespVo checkout(@CurrentUser Integer userId, WxCartFastAddBo wxCartFastAddBo){
        if (wxCartFastAddBo.getAddressId()==0){
            return BaseRespVo.fail("地址不能为空");
        }
        WxCheckoutVO checkout = wxCartService.checkout(userId, wxCartFastAddBo);
        return BaseRespVo.ok(checkout);
    }
}